package it.uniroma2.art.coda.main;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.apache.uima.UIMAFramework;
import org.apache.uima.analysis_engine.AnalysisEngine;
import org.apache.uima.analysis_engine.AnalysisEngineProcessException;
import org.apache.uima.cas.FSIndex;
import org.apache.uima.cas.Feature;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.Type;
import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.tcas.Annotation;
import org.apache.uima.resource.ResourceInitializationException;
import org.apache.uima.resource.ResourceSpecifier;
import org.apache.uima.util.InvalidXMLException;
import org.apache.uima.util.XMLInputSource;

public class AnnotationPrintTest {

	
	//private static CODACore codaCore = null;
	//private static String aggregateAEFilePath = "E:/CODATest/provaAggregateAE.xml"; // aggregate nomi propri + email
	//private static String aggregateAEFilePath = "E:/CODATest/provaAggregateAEPub.xml"; // aggregate per pubblcazioni
	private static String aggregateAEFilePath = "E:/CODATest/Famiglia/desc/aeDescriptor.xml"; // simple AE famiglia
	
	
	//private static String txtFilePath = "E:/CODATest/test.txt";
	//private static String htmlFilePath = "E:/CODATest/andreaRedux.html";
	private static String txtFilePath = "E:/CODATest/famiglia.txt";
	
	//private static String dirComponentsPath = "E:/CODATest/components";
	
	private static String textToBeAnnotatate;
	
	//private static Collection <JCas> jcasCollection = new ArrayList<JCas>();
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		try {
			
			//read the input file containig the test to be analized
			FileReader fir = new FileReader(txtFilePath);
			//FileReader fir = new FileReader(htmlFilePath);
			BufferedReader  br = new BufferedReader (fir); 
			
			StringBuilder contents = new StringBuilder();
			String line;
			
			while (( line = br.readLine()) != null){
	          contents.append(line);
	          contents.append(System.getProperty("line.separator"));
	        }
			textToBeAnnotatate = contents.toString();

			XMLInputSource in;
			ResourceSpecifier specifier;
			AnalysisEngine ae;
			JCas annotCas;
			in = new XMLInputSource(aggregateAEFilePath);
			specifier = UIMAFramework.getXMLParser().parseResourceSpecifier(in);
			ae = UIMAFramework.produceAnalysisEngine(specifier);
			ae.setConfigParameterValue("Encoding", "UTF-8");
			annotCas = ae.newJCas();
			annotCas.setDocumentText(textToBeAnnotatate);
			annotCas.setDocumentLanguage("it");
			ae.process(annotCas);
			
			
			FSIndex indexAnnotations = annotCas.getAnnotationIndex();
			Iterator<Annotation> annoIt = indexAnnotations.iterator();
			//Annotation ann = annoIt.next();
			annoIt.next();
			//System.out.println("testo analizzato: "+ann.getCoveredText());
			while(annoIt.hasNext()){
				Annotation annotation = annoIt.next();
				String text = annotation.getCoveredText();
				int begin = annotation.getBegin();
				int end = annotation.getEnd();
				String type = annotation.getType().getName();
				System.out.println("text: "+text+"\tbegin: "+begin+"\tend:"+end+"\ttype: "+type);
				Type typeUIMA = annotation.getType();
				
				(new AnnotationPrintTest()).printTypeSystem(typeUIMA, annotation, 1);
				
				
				
			}
			System.out.println("FINE stampa annotazioni");	
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidXMLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ResourceInitializationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AnalysisEngineProcessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	//public void printTypeSystem(Type uimaType, Annotation annotation, int level){
	public void printTypeSystem(Type uimaType, FeatureStructure featStruct, int level){
		if(featStruct == null)
			return;
		String levelString="\t";
		for (int i=0; i<level; ++i)
			levelString += "\t";
		List <Feature> feauturesList = uimaType.getFeatures();
		if(level == 1)
			System.out.println("T.S. = "+uimaType.getName());
		for(int i=0; i<feauturesList.size(); ++i){
			Feature feature = (Feature) feauturesList.get(i);
			String nameFeat = feature.getName();
			if(!nameFeat.startsWith("uima.")){
				Type typeRange = feature.getRange();
				if(typeRange.isPrimitive()){
					String value = featStruct.getFeatureValueAsString(feature);
					System.out.println(levelString+"F: "+nameFeat+" = "+" V: "+value);
				}
				else{
					System.out.println(levelString+"F: "+nameFeat+" = "+" TS: "+typeRange.getName());
					printTypeSystem(typeRange, featStruct.getFeatureValue(feature), level+1);
				}
			}
		}
	}
}
